/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Search.java
 *
 * Created on Dec 20, 2008, 4:54:52 PM
 */

package GUI;

import Dictionary.Dictionary;
import functions.Dogy;
import functions.Users;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import model.Dog;
import model.User;

/**
 *
 * @author GiK
 */
public class Search extends javax.swing.JPanel {
    // @Exception handler
    private MainFrame frame;
    private String namesText, breedText, addressText, dogNickText;
    /** Creates new Search form */
    
    public Search(MainFrame mainFr) {
        initComponents();
        
        frame = mainFr;
        namesText = Dictionary.NAME_OR_SURNAME;
        breedText = Dictionary.BREED;
        addressText = Dictionary.ADDRESS;
        dogNickText = Dictionary.NICKNAME;
        nameTextField.setText(namesText);
        nameTextField.setToolTipText(namesText);
        breedTextField.setText(breedText);
        breedTextField.setToolTipText(breedText);
        addressTextField.setText(addressText);
        addressTextField.setToolTipText(addressText);
        nickTextField.setText(dogNickText);
        nickTextField.setToolTipText(dogNickText);

        nameTextField.setForeground(Color.gray);
        breedTextField.setForeground(Color.gray);
        addressTextField.setForeground(Color.gray);
        nickTextField.setForeground(Color.gray);

        javax.swing.JMenuItem menuItem = new javax.swing.JMenuItem("Обьеденить/Перенести");
        menuItem.setFont(new java.awt.Font("Tahoma", 1, 12));
        final Search sch = this;
        menuItem.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                //if(usersTable.getSelectedRowCount() > 1)
                //{
                   ArrayList<User> users = new ArrayList<User>();
                   for(int i=0; i< usersTable.getSelectedRows().length; i++)
                       users.add((User)usersTable.getValueAt(usersTable.getSelectedRows()[i], 1));
                   Unite un = new Unite(frame,users,sch);
                   un.setVisible(true);
                //}
            }

        });

        popupMenu.add(menuItem);

        //menuItem = new javax.swing.JMenuItem("Assign Voice print");


        //popupMenu.add(menuItem);
        
        popupMenu.setSize(150, 50);
        popupMenu.pack();
        
    }
private boolean nameFieldIsEmpty()
{
    return nameTextField.getText().equals(namesText) ||
                nameTextField.getText().equals("");
}
private boolean nickFieldIsEmpty()
{
    return nickTextField.getText().equals(dogNickText) ||
                nickTextField.getText().equals("");
}
private boolean addressIsEmpty()
{
    return addressTextField.getText().equals(addressText) ||
                addressTextField.getText().equals("");
}
private boolean breedIsEmpty()
{
    return breedTextField.getText().equals(breedText) ||
                breedTextField.getText().equals("");
}
    public void fillTable(ArrayList<User> users) {
        DefaultTableModel model = (DefaultTableModel) usersTable.getModel();
        model.setRowCount(0);

        //fast
        /*if(nameFieldIsEmpty() && nickFieldIsEmpty() && addressIsEmpty() && breedIsEmpty())
        for(int i=0;i< users.size(); i++)
        {
            Dog dog = users.get(i).getDog();
            model.addRow(new Object[]{users.get(i),
                                      users.get(i).getName(),
                                      users.get(i).getStreet(),
                                      users.get(i).getDogId() == 0 ? "Нет животных":
                                      users.get(i).getDogName()});
        }
        else*/
        // full
        for(int i=0;i< users.size(); i++)
        {
            Dog dog = users.get(i).getDog();
            model.addRow(new Object[]{users.get(i).getId(),
                                      users.get(i),
                                      users.get(i).getStreet(),
                                      dog == null ? "Нет животных":
                                      dog.toString()});
        }
        usersTable.setModel(model);
    }
    public void refreshSearch()
    {
        int [] selRows = usersTable.getSelectedRows();
        searchButtonActionPerformed(null);
        for(int i=0; i< selRows.length; i++)
          usersTable.getSelectionModel().addSelectionInterval(selRows[i], selRows[i]);
    }

    public void showUserWithName(String name)
    {
        nameTextField.setText(name);
        nameTextField.requestFocus();
        searchButtonActionPerformed(null);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupMenu = new javax.swing.JPopupMenu();
        jScrollPane1 = new javax.swing.JScrollPane();
        usersTable = new javax.swing.JTable();
        nameTextField = new javax.swing.JTextField();
        breedTextField = new javax.swing.JTextField();
        openCardButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();
        nickTextField = new javax.swing.JTextField();
        addressTextField = new javax.swing.JTextField();
        groupCheck = new javax.swing.JCheckBox();
        addUser = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        popupMenu.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        setMaximumSize(new java.awt.Dimension(704, 418));
        setMinimumSize(new java.awt.Dimension(704, 418));
        setPreferredSize(new java.awt.Dimension(704, 418));
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        usersTable.setAutoCreateRowSorter(true);
        usersTable.setFont(usersTable.getFont().deriveFont(usersTable.getFont().getStyle() | java.awt.Font.BOLD, usersTable.getFont().getSize()+3));
        usersTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

                "ID",
                Dictionary.NAME + " " +Dictionary.SURNAME,
                Dictionary.ADDRESS,
                Dictionary.PET
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        usersTable.setGridColor(new java.awt.Color(1, 1, 1));
        usersTable.setMaximumSize(new java.awt.Dimension(300, 72));
        usersTable.setMinimumSize(new java.awt.Dimension(300, 72));
        usersTable.setRowHeight(30);
        usersTable.setShowVerticalLines(false);
        usersTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                usersTableMouseClicked(evt);
            }
        });
        usersTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                usersTableKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                usersTableKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(usersTable);
        usersTable.getColumnModel().getColumn(0).setPreferredWidth(50);
        usersTable.getColumnModel().getColumn(0).setMaxWidth(60);
        DefaultTableCellRenderer end = new DefaultTableCellRenderer();
        end.setHorizontalTextPosition(end.LEFT);
        usersTable.setDefaultRenderer(usersTable.getColumnClass(0), end);

        nameTextField.setFont(new java.awt.Font("Tahoma", 0, 14));
        nameTextField.setText("<html><b>Имя или Фамилия</b>");
        nameTextField.setToolTipText("Имя или Фамилия клиента");
        nameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameTextFieldActionPerformed(evt);
            }
        });
        nameTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                nameTextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                nameTextFieldFocusLost(evt);
            }
        });
        nameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nameTextFieldKeyPressed(evt);
            }
        });

        breedTextField.setFont(new java.awt.Font("Tahoma", 0, 14));
        breedTextField.setText("Порода");
        breedTextField.setToolTipText("Порода");
        breedTextField.setNextFocusableComponent(addressTextField);
        breedTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                breedTextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                breedTextFieldFocusLost(evt);
            }
        });
        breedTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                breedTextFieldKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                breedTextFieldKeyReleased(evt);
            }
        });

        openCardButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        openCardButton.setText(Dictionary.OPEN_CARD);
        openCardButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openCardButtonActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jButton2.setText(Dictionary.CHANGE);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        searchButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        searchButton.setText(Dictionary.SEARCH);
        searchButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                searchButtonMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                searchButtonMousePressed(evt);
            }
        });
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });
        searchButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                searchButtonKeyPressed(evt);
            }
        });

        nickTextField.setFont(new java.awt.Font("Tahoma", 0, 14));
        nickTextField.setText("Кличка");
        nickTextField.setNextFocusableComponent(searchButton);
        nickTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                nickTextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                nickTextFieldFocusLost(evt);
            }
        });
        nickTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nickTextFieldKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nickTextFieldKeyReleased(evt);
            }
        });

        addressTextField.setFont(new java.awt.Font("Tahoma", 0, 14));
        addressTextField.setText("Адрес");
        addressTextField.setToolTipText("Адрес");
        addressTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                addressTextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                addressTextFieldFocusLost(evt);
            }
        });
        addressTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                addressTextFieldKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                addressTextFieldKeyReleased(evt);
            }
        });

        groupCheck.setFont(new java.awt.Font("Tahoma", 1, 14));
        groupCheck.setSelected(true);
        groupCheck.setText(Dictionary.GROUPING);
        groupCheck.setToolTipText("Группировать пользователей по имени и фамилии, если у них несколько животных");

        addUser.setFont(new java.awt.Font("Tahoma", 1, 14));
        addUser.setText(Dictionary.ADD);
        addUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addUserActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jButton1.setText(Dictionary.DELETE);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton4.setFont(new java.awt.Font("Tahoma", 1, 14));
        jButton4.setText("Перенести");
        jButton4.setMargin(new java.awt.Insets(2, 10, 2, 10));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
                            .addComponent(addressTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(nickTextField)
                            .addComponent(breedTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(searchButton, 0, 0, Short.MAX_VALUE)
                            .addComponent(groupCheck, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addUser, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                        .addComponent(openCardButton, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addressTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(breedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(groupCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nickTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(addUser, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(openCardButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nameTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFieldFocusLost
        if(nameTextField.getText().equals(""))
        {
        nameTextField.setText(namesText);
        nameTextField.setForeground(Color.gray);
        }
}//GEN-LAST:event_nameTextFieldFocusLost

    private void breedTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_breedTextFieldFocusGained
        if(breedTextField.getText().equals("") ||
                breedTextField.getText().equals(breedText))
            breedTextField.setText("");
        breedTextField.setForeground(Color.black);
}//GEN-LAST:event_breedTextFieldFocusGained

    private void breedTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_breedTextFieldFocusLost
        if(breedTextField.getText().equals(""))
            {
        breedTextField.setText(breedText);
        breedTextField.setForeground(Color.gray);
        }
}//GEN-LAST:event_breedTextFieldFocusLost

    private void searchButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchButtonKeyPressed

        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
            searchButton.doClick();
        
}//GEN-LAST:event_searchButtonKeyPressed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        //searchButton.setText("Загрузка");
        //searchButton.updateUI();
        Users searchUsers = new Users();
        if ( groupCheck.isSelected()) searchUsers.groupByName();
        if( !nameTextField.getText().equals(namesText) &&
                !nameTextField.getText().equals(""))
            searchUsers.searchByName(nameTextField.getText());
        if( !addressTextField.getText().equals(addressText) &&
                !addressTextField.getText().equals(""))
            searchUsers.searchByAddress(addressTextField.getText());
        if( !breedTextField.getText().equals(breedText) &&
                !breedTextField.getText().equals(""))
            searchUsers.searchByBreed(breedTextField.getText());
        if( !nickTextField.getText().equals(dogNickText) &&
                !nickTextField.getText().equals(""))
            searchUsers.searchByNick(nickTextField.getText());
        try {
            ArrayList<User> users = searchUsers.startSearch();
            fillTable(users);
            searchButton.setText(Dictionary.SEARCH);
        } catch (SQLException ex) {
            frame.SQLException(ex);
        }
}//GEN-LAST:event_searchButtonActionPerformed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed

    }//GEN-LAST:event_formKeyPressed

    private void nameTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameTextFieldKeyPressed

        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            searchButton.setText("Загрузка");
            searchButton.doClick();
        }
        if(evt.getKeyCode() == KeyEvent.VK_DOWN)
        {
            usersTable.setRowSelectionInterval(0, 0);
            usersTable.requestFocus();
        }
}//GEN-LAST:event_nameTextFieldKeyPressed

    private void breedTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_breedTextFieldKeyPressed
        
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            searchButton.setText("Загрузка");
            searchButton.doClick();
        }
            
}//GEN-LAST:event_breedTextFieldKeyPressed

    private void nickTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nickTextFieldFocusGained
        if(nickTextField.getText().equals("") ||
                nickTextField.getText().equals(dogNickText))
        nickTextField.setText("");
        nickTextField.setForeground(Color.black);
}//GEN-LAST:event_nickTextFieldFocusGained

    private void nickTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nickTextFieldFocusLost
        if(nickTextField.getText().equals(""))
        {
            nickTextField.setText(dogNickText);
            nickTextField.setForeground(Color.gray);
        }
        
}//GEN-LAST:event_nickTextFieldFocusLost

    private void nickTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nickTextFieldKeyPressed
        
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            searchButton.setText("Загрузка");
            searchButton.doClick();
        }
            
}//GEN-LAST:event_nickTextFieldKeyPressed

    private void addressTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_addressTextFieldFocusLost
         if(addressTextField.getText().equals(""))
         {
            addressTextField.setText(addressText);
            addressTextField.setForeground(Color.gray);
         }
}//GEN-LAST:event_addressTextFieldFocusLost

    private void addressTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_addressTextFieldKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            searchButton.setText("Загрузка");
            searchButton.doClick();
        }
            
}//GEN-LAST:event_addressTextFieldKeyPressed

    private void addressTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_addressTextFieldFocusGained
        if(addressTextField.getText().equals("") ||
                addressTextField.getText().equals(addressText))
        addressTextField.setText("");
        addressTextField.setForeground(Color.black);
}//GEN-LAST:event_addressTextFieldFocusGained

    private void openCardButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openCardButtonActionPerformed
        if(usersTable.getSelectedRow() != -1)
        {
          User selUser = (User)usersTable.getValueAt(usersTable.getSelectedRow(), 1);
          UserCard userCard = new UserCard(frame,selUser);
          userCard.setVisible(true);
        }
}//GEN-LAST:event_openCardButtonActionPerformed

    private void nameTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFieldFocusGained

        if(nameTextField.getText().equals("") ||
                nameTextField.getText().equals(namesText))
        nameTextField.setText("");
         nameTextField.setForeground(Color.black);
        
    }//GEN-LAST:event_nameTextFieldFocusGained

    private void nameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nameTextFieldActionPerformed

    private void searchButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_searchButtonMousePressed
        //searchButton.setText("Загрузка");
    }//GEN-LAST:event_searchButtonMousePressed

    private void searchButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_searchButtonMouseClicked
       // searchButton.setText("Загрузка");
    }//GEN-LAST:event_searchButtonMouseClicked

    private void addUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addUserActionPerformed
        AddNewUser addNew = new AddNewUser(frame,null,this);
        addNew.setVisible(true);
    }//GEN-LAST:event_addUserActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        User user;
        if(usersTable.getSelectedRow() != -1)
        {
            user = (User) usersTable.getValueAt(usersTable.getSelectedRow(), 1);
            System.out.println(user.getId());
            AddNewUser addNew = new AddNewUser(frame,user,this);
            addNew.setVisible(true);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void usersTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_usersTableMouseClicked
        if(evt.getClickCount() >= 2)
        openCardButtonActionPerformed(null);

        if(evt.getButton() == MouseEvent.BUTTON3)
        {
            popupMenu.show(usersTable, evt.getX(), evt.getY());
            popupMenu.setVisible(true);

        }
    }//GEN-LAST:event_usersTableMouseClicked

    private void usersTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usersTableKeyReleased
        

    }//GEN-LAST:event_usersTableKeyReleased

    private void addressTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_addressTextFieldKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_DOWN)
        {
            usersTable.setRowSelectionInterval(0, 0);
            usersTable.requestFocus();
        }
    }//GEN-LAST:event_addressTextFieldKeyReleased

    private void breedTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_breedTextFieldKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_DOWN)
        {
            usersTable.setRowSelectionInterval(0, 0);
            usersTable.requestFocus();
        }
    }//GEN-LAST:event_breedTextFieldKeyReleased

    private void nickTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nickTextFieldKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_DOWN)
        {
            usersTable.setRowSelectionInterval(0, 0);
            usersTable.requestFocus();
        }
    }//GEN-LAST:event_nickTextFieldKeyReleased

    private void usersTableKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usersTableKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
           openCardButtonActionPerformed(null);
    }//GEN-LAST:event_usersTableKeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        int [] selRows = usersTable.getSelectedRows();
        if(selRows.length == 0)
        {
            frame.FormException(this,
                    "Выделите клиента которого хотите удалить.");
            return;
        }
        boolean res = frame.confirmForm(this, "<html>Вы действительно хотите удалить <b>"+selRows.length +"</b>"
                + (selRows.length  == 1 ? " клиента":" клиентов"));
        ArrayList toDelete = new ArrayList();
        if(res)
        try
        {
           DefaultTableModel model = (DefaultTableModel)usersTable.getModel();
           for(int i=0; i < selRows.length; i++)
           {
             toDelete.add(usersTable.getValueAt(selRows[i]-i, 0));
             model.removeRow(selRows[i]-i);
           }
           Users.delete(toDelete);
           usersTable.setModel(model);
        }catch(SQLException e)
        {
            frame.SQLException(e);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        final Search sch = this;
        ArrayList<User> users = new ArrayList<User>();
        if(usersTable.getSelectedRows().length > 0)
          for(int i=0; i< usersTable.getSelectedRows().length; i++)
             users.add((User)usersTable.getValueAt(usersTable.getSelectedRows()[i], 1));
        Unite un = new Unite(frame,users,this);
        un.setActiveTab(1);
        un.setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addUser;
    private javax.swing.JTextField addressTextField;
    private javax.swing.JTextField breedTextField;
    private javax.swing.JCheckBox groupCheck;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JTextField nickTextField;
    private javax.swing.JButton openCardButton;
    private javax.swing.JPopupMenu popupMenu;
    private javax.swing.JButton searchButton;
    private javax.swing.JTable usersTable;
    // End of variables declaration//GEN-END:variables

}
